package com.hemerasolutions.v2trainer.component.list;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.list.ModalityIZListCompInterface;
import com.hemerasolutions.v2trainer.model.exercise.executions.Execution;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityModTable;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityModZone;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.unit.intensity.Rpe;
import com.hemerasolutions.v2trainer.service.interfaces.ModalityServiceInterface;

@Controller
@Component("modalityIZListComp")
@Scope("prototype")
public class ModalityIZListComp extends BaseComponent implements ModalityIZListCompInterface {

	private static final long serialVersionUID = 1L;

	private Modality modality;
	
	private IntensityModTable selIntensityModTable;

	private IntensityModZone selIntensityModZone;

	private List<Rpe> fullRpes;
	
	@Autowired
	private ModalityServiceInterface modalityService;

	
	
	@Override
	public void refresh() {
		this.fullRpes = this.modalityService.listAllRpes();
		this.loadModalityZones();
		
	}

   private void loadModalityZones() {
		boolean imFounded = false;
		boolean exFounded = false;
		
		List<IntensityModTable> temp = new ArrayList<IntensityModTable>();
				
		for(IntensityModTable imAux:this.modality.getIntensityModTables()){
			exFounded = false;
			for(Execution exAux:this.modality.getExecutions()){
				if(imAux.getExecution().getName().equals(exAux.getName())){
					exFounded = true;
					break;
				}
			}
			if(!exFounded){
				temp.add(imAux);
			}
		}
		
		this.modality.getIntensityModTables().removeAll(temp);
		
		for(Execution exAux:this.modality.getExecutions()){
			imFounded = false;
			for(IntensityModTable imAux:this.modality.getIntensityModTables()){
				if(imAux.getExecution().getName().equals(exAux.getName())){
					imFounded = true;
					break;
				}
			}
			if(!imFounded){
				IntensityModTable im = new IntensityModTable();
				im.setExecution(exAux);
				im.setModality(this.modality);
				this.modality.getIntensityModTables().add(im);
			}
		}
	}

@Override
public void toAddModalityItensityZone(){
    	this.setSelIntensityModZone(this.selIntensityModTable.addIntensityModZone());
    	if(this.getSelIntensityModZone().getPhysicalCapacity() == null){
    		this.getSelIntensityModZone().setPhysicalCapacity(this.modality.getTrainingType().getPhysicalCapacitys().get(0));
    	}    
    }
    	
    @Override
	public void upModalityItensityZone(){
    	this.selIntensityModTable.upIntensityModZone(this.selIntensityModZone);
    }
    
    @Override
	public void downModalityItensityZone(){
    	this.selIntensityModTable.downIntensityModZone(this.selIntensityModZone);
    	
    }

    @Override
	public void deleteModalityItensityZone(){
    	this.selIntensityModTable.getIntensityModZones().remove(this.selIntensityModZone);
    }	



	@Override
	public Modality getModality() {
		return modality;
	}


	@Override
	public void setModality(Modality modality) {
		this.modality = modality;
	}


	@Override
	public IntensityModTable getSelIntensityModTable() {
		return selIntensityModTable;
	}


	@Override
	public void setSelIntensityModTable(IntensityModTable selIntensityModTable) {
		this.selIntensityModTable = selIntensityModTable;
	}


	@Override
	public IntensityModZone getSelIntensityModZone() {
		return selIntensityModZone;
	}


	@Override
	public void setSelIntensityModZone(IntensityModZone selIntensityModZone) {
		this.selIntensityModZone = selIntensityModZone;
	}


	@Override
	public List<Rpe> getFullRpes() {
		return fullRpes;
	}


	@Override
	public void setFullRpes(List<Rpe> fullRpes) {
		this.fullRpes = fullRpes;
	}
	
	
	

}
